﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Xml;

namespace Marker
{
    public partial class TypesDictionaryForm : Form
    {
        private List<string> typesList;
        private List<Color> colorList;
        private bool skalujObraz;
        private bool automatycznyZapis;
        private int maxIloscPunktow;

        public TypesDictionaryForm(List<string> typesList, List<Color> colorList, bool skaluj, bool autoZapis, int max)
        {
            InitializeComponent();
            this.typesList = typesList;
            this.colorList = colorList;
            this.skalujObraz = skaluj;
            this.automatycznyZapis = autoZapis;
            this.maxIloscPunktow = max;
            RefreshGrid();

            cbDopasuj.Checked = this.skalujObraz;
            cbAutomatycznyZapis.Checked = this.automatycznyZapis;
            txtMax.Text = this.maxIloscPunktow.ToString();

        }

        private void RefreshGrid()
        {
            gvTypes.Rows.Clear();
            for (int i = 0; i < typesList.Count; i++)
            {
                gvTypes.Rows.Add(typesList[i], colorList[i].Name.ToString());
                gvTypes.Rows[i].Cells[1].Style.BackColor = colorList[i];
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtTypeName.Text != String.Empty)
            {
                typesList.Add(txtTypeName.Text);
                RefreshGrid();
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (gvTypes.SelectedRows.Count > 0)
            {
                foreach(DataGridViewRow row in gvTypes.SelectedRows)
                {
                    typesList.RemoveAt(row.Index);
                }
            }
            RefreshGrid();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (Save())
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            else
                this.DialogResult = System.Windows.Forms.DialogResult.Abort;            
            this.Close();
        }

        private bool Save()
        {
            bool result = false;

            try
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.IndentChars = ("    ");
                using (XmlWriter writer = XmlWriter.Create(Application.ProductName + ".exe.config", settings))
                {
                    writer.WriteStartElement("configuration");

                    //typy
                    writer.WriteStartElement("pointTypes");
                    foreach (string type in this.typesList)
                    {
                        writer.WriteStartElement("type");
                        writer.WriteAttributeString("name", type);
                        writer.WriteEndElement();
                    }                  
                    writer.WriteEndElement();

                    //userSettings
                    writer.WriteStartElement("userSettings");

                    writer.WriteStartElement("setting");
                    writer.WriteAttributeString("name", "skalujObraz");
                    writer.WriteAttributeString("value", this.skalujObraz.ToString());
                    writer.WriteEndElement();

                    writer.WriteStartElement("setting");
                    writer.WriteAttributeString("name", "maxIloscPunktow");
                    writer.WriteAttributeString("value", this.maxIloscPunktow.ToString());
                    writer.WriteEndElement();

                    writer.WriteStartElement("setting");
                    writer.WriteAttributeString("name", "automatycznyZapis");
                    writer.WriteAttributeString("value", this.automatycznyZapis.ToString());
                    writer.WriteEndElement();

                    writer.WriteEndElement();

                    writer.WriteEndElement();

                    writer.Flush();
                    result = true;
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show("Nie udało się zapisac zmian!");                
            }

            return result;
        }

        private void cbAutomatycznyZapis_CheckedChanged(object sender, EventArgs e)
        {
            this.automatycznyZapis = cbAutomatycznyZapis.Checked;            
        }

        private void cbDopasuj_CheckedChanged(object sender, EventArgs e)
        {
            this.skalujObraz = cbDopasuj.Checked;
        }

        private void txtMax_TextChanged(object sender, EventArgs e)
        {
            int nr;
            if (int.TryParse(txtMax.Text, out nr))
                this.maxIloscPunktow = nr;
            else
                txtMax.Text = this.maxIloscPunktow.ToString();
        }
    }
}
